Thread: The "||" function

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    congratulations your first lesson in debugging - look through you code and find where you would get the value -32. forget about what values variables are suposed to be.

    also, the inner set of {} is unnecessary


    edit: scribbler beat me to this
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #17
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I believe your problem is right on this line: float b = ( 5/9 * a - 32 ) ;

    More precisly 5/9, that is integer division and it is my bet that this is evaluated to 0, so basicly your expression looks like this:
    float b = ( 0 * a - 32 ) ;

    To fix you have to make sure you tell the compiler that you dont want integer division, by doing this: float b = ( 5.0f/9.0f * a - 32 ) ;

    The trailing f is just to tell the compiler its a float and not a double.

    Edit: awwwwww beaten!

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    I got it, finaly, thanks

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    actually, it was that i didnt have "()" around "a - 32", it did the multiplication first, thats why I got that answer
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
        
        cout<<"Enter Farenheit temperature to convert to Celcius:\n";
        cin>> a;
        float b = (5.0/9.0 * (a - 32) ) ;
        cin.ignore();
        {
                   cout<<"Your answer is:"<< b <<"\n";
        }
             cin.get();
    }

  5. #20
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by futurama140
    actually, it was that i didnt have "()" around "a - 32", it did the multiplication first, thats why I got that answer
    i doubt that. the output was -32 every time. what possible values are in the expression (n * m - 32) in which return the value -32? we know that changing 'm' does not effect the outcome, so we have (n * const value - 32). n = 5/9. if n is evalutated as an integer, then n = 0. which makes (0 * const value - 32). (0 * any number - 32) is always - 32.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Both were problem. Solving only the first part would have resulted in values that actually depend on the input, but they would have been false. Solving only the second part would have resulted in 0 always being the output instead of -32.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM